//
//  ZTSystem.m
//  ZHYYTool
//
//  Created by MT-SW-005 on 2021/9/28.
//  Copyright © 2021 MT-SW-005. All rights reserved.
//

#import "ZTSystem.h"

@implementation ZTSystem

+(NSString*)runNSTaskCmd:(NSString*)cmd
{
    NSTask *task = [[NSTask alloc] init] ;
    NSPipe *outpipe = [NSPipe pipe] ;
    [task setLaunchPath:@"/bin/sh"] ;
    
    NSArray *argument = [NSArray arrayWithObjects:@"-c",cmd,nil] ;
    [task setArguments:argument] ;
    [task setStandardError:outpipe] ;
    [task setStandardOutput:outpipe] ;
    [task launch] ;
    [task waitUntilExit] ;
    NSData *data = [[outpipe fileHandleForReading] readDataToEndOfFile] ;
    NSString *strReceive = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    return strReceive;
}



/*
 if(int status !-1
    && WIFEXITED(status)==YES
    && WEXITSTATUS(status)==0)
 {
    return YES
 };
 */
+(BOOL)runSystem:(NSString*)cmd {
    BOOL executed = NO;
    int status = system([NSString stringWithFormat:@"%@",cmd].UTF8String);
    if (status!=-1 && WIFEXITED(status)==YES && WEXITSTATUS(status)==0) {
        executed = YES;
    }
    return executed;
}
@end
